package cn.b504.qpcr.controller;

import cn.b504.qpcr.entity.report.Report;
import cn.b504.qpcr.interfaces.ReportService;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/reportAction")
@Scope(value = WebApplicationContext.SCOPE_SESSION)
public class ReportController {
  private final ReportService reportService;

  public ReportController(ReportService reportService) {
    this.reportService = reportService;
  }

  private WordprocessingMLPackage wordprocessingMLPackage;
  private String experimentName;
  private String exportType;

  @RequestMapping("/generate")
  public void generate(@RequestBody Report report) {
    switch (report.getLanguage()) {
      case "English":
        wordprocessingMLPackage = reportService.generateFile(report);
        break;
      case "Chinese":
        wordprocessingMLPackage = reportService.generateFileWithChinese(report);
        break;
    }
    exportType = report.getType();
    experimentName = report.getHeader().getTitle();
  }

  @RequestMapping("/generateUrl")
  public void generate_url(HttpServletResponse response) {
    if (wordprocessingMLPackage != null) {
      reportService.export(response, wordprocessingMLPackage, experimentName, exportType);
    }
  }
}
